package cn.itcast.mq.listener;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * Author: Zheng
 * Date:   2024/3/19  10:07
 * Package：cn.itcast.mq.listener
 * Description：
 * Version：1.0
 */
@Component
public class SpringRabbitListener {

    /**
     * @description： 监听基本消息队列消息
     * @date：2024/3/19 14:53
     * @return：
     **/
    //@RabbitListener(queues = "simple.queue")
    //public void listenBasicQueue(String msg) {
    //    System.out.println("基本消息队列接收到消息：" + msg);
    //}

    /**
     * @description： 监听工作消息队列 消费者1
     * @date：2024/3/19 14:55
     * @return：
     **/
    @RabbitListener(queues = "simple.queue")
    public void listenWorkQueue1(String msg) throws InterruptedException {
        System.out.println("工作消息队列111111111接收到消息：" + msg);
        Thread.sleep(25);
    }

    /**
     * @description： 监听工作消息队列 消费者2
     * @date：2024/3/19 14:56
     * @return：
     **/
    @RabbitListener(queues = "simple.queue")
    public void listenWorkQueue2(String msg) throws InterruptedException {
        System.out.println("工作消息队列2222222222接收到消息：" + msg);
        Thread.sleep(100);
    }

    /**
     * @description： 监听广播模式队列消息 消费者1
     * @date：2024/3/19 15:37
     * @return：
     **/
    @RabbitListener(queues = "fanout.queue1")
    public void listenFanoutQueue1(String msg) {
        System.out.println("接收到fanout11111队列的消息：" + msg);
    }

    /**
     * @description： 监听广播模式队列消息 消费者2
     * @date：2024/3/19 15:37
     * @return：
     **/
    @RabbitListener(queues = "fanout.queue2")
    public void listenFanoutQueue2(String msg) {
        System.out.println("接收到fanout22222队列的消息：" + msg);
    }

    /**
     * @description： 路由模式订阅消息，通过注解方式声明队列和交换机
     * @date：2024/3/19 15:58
     * @return：
     **/
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name = "direct.queue1"),
                    exchange = @Exchange(name = "itcast.direct", type = ExchangeTypes.DIRECT),
                    key = {"red", "blue"}
            ))
    public void listenDirectQueue1(String msg) {
        System.out.println("接收到direct队列1111的消息：" + msg);
    }

    /**
     * @description： 路由模式订阅消息，注解方式声明队列和交换机
     * @date：2024/3/19 16:00
     * @return：
     **/
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name = "direct.queue2"),
                    exchange = @Exchange(name = "itcast.direct", type = ExchangeTypes.DIRECT),
                    key = {"red", "yellow"}
            ))
    public void listenDirectQueue2(String msg) {
        System.out.println("接收到direct队列2222的消息：" + msg);
    }

    /**
     * @description： 主题模式订阅消息，交换机通过匹配路由key规则进行队列消息投递
     * @date：2024/3/19 16:20
     * @return：
     **/
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name = "topic.queue1"),
                    exchange = @Exchange(name = "itcast.topic", type = ExchangeTypes.TOPIC),
                    key = {"china.#"}
            ))
    public void listenOpticQueue1(String msg) {
        System.out.println("接收到topic队列11111的消息：" + msg);
    }

    /**
     * @description： 主题模式订阅消息，交换机通过匹配路由key规则进行队列消息投递
     * @date：2024/3/19 16:22
     * @return：
     **/
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name = "topic.queue2"),
                    exchange = @Exchange(name = "itcast.topic", type = ExchangeTypes.TOPIC),
                    key = {"#.news"}
            ))
    public void listenOpticQueue2(String msg) {
        System.out.println("接收到topic队列22222的消息：" + msg);
    }

    /**
     * @description： 接收对象类型消息队列消息
     * @date：2024/3/19  16:49
     * @return：
     **/
    @RabbitListener(queues = "object.queue")
    public void listenObjQueue(Map<String,Object> map){
        System.out.println("接收到对象类型消息："+map.get("name")+"--->"+map.get("age"));
    }
}
